<?php
namespace Human\Controller;
use Admin\Controller\AdminController;
/**
 * 员工管理
 */

class EmployeeController extends AdminController {
    /**
     * 当前模块参数
     */
    protected function _infoModule(){
        return array(
            'info'  => array(
                'name' => '所有员工',
                'description' => '管理员工',
                ),
            'menu' => array(
                array('name' => '员工列表',
                    'url' => U('Human/employee/index'),
                    'icon' => 'list',
                    ),
                ),
            'add' => array(
                array('name' => '添加员工',
                    'url' => U('add'),
                    ),
                ),
            );
    }
	/**
     * 员工
     */
    public function index(){

        //筛选条件
        $where = array();
        $keyword = trim(I('request.keyword',''));
        if(!empty($keyword)){
            $where['_string'] = ' (A.employee_name like "%'.$keyword.'%") OR ( A.employee_real_name like "%'.$keyword.'%") OR ( A.employee_email like "%'.$keyword.'%") ';
        }
        $departmentId = I('request.department_id','');
        if(!empty($departmentId)){
            $where['A.department_id'] = $departmentId;
        }
        $breadCrumb = array('员工列表' => U());
        $this->assign('breadCrumb', $breadCrumb);
        $count = D('employee')->countList();
        $limit = $this->getPageLimit($count,10);
        $this->assign('list', D('employee')->loadList($where,$limit)); 
        $this->assign('page',$this->getPageShow($pageMaps));
        $this->assign('departmentList',D('Department')->loadList());
        $this->assign('keyword',$keyword);
        $this->assign('departmentId',$departmentId);
        $this->adminDisplay();
    }

    /**
     * 增加
     */
    public function add()
    {
        if (!IS_POST)
        {
            $breadCrumb = array('员工列表' => U('index'), '员工添加' => U());
            $this->assign('breadCrumb', $breadCrumb);
            $this->assign('name', '添加');
            $departmentList = D('Human/Department') ->loadList();
            $this->assign('departmentList', $departmentList);
            $this->assign('storeSearch',D('Functions/Store')->loadList());
            $this->adminDisplay('info');
        }
        else
        {
            $model = D('employee');
            if ($model->saveData('add'))
            {
                $this->success('员工添加成功！');
            }
            else
            {
                $msg = $model->getError();
                if (empty($msg))
                {
                    $this->error('员工添加失败');
                }
                else
                {
                    $this->error($msg);
                }
            }
        }
    }

    /**
     * 修改
     */
    public function edit()
    {
        $model = D('employee');
        if (!IS_POST)
        {
            $employeeId = I('get.employee_id', '', 'intval');
            if (empty($employeeId))
            {
                $this->error('参数不能为空！');
            }
            $info = $model->getInfo($employeeId);
            if (!$info)
            {
                $this->error($model->getError());
            }
            $breadCrumb = array('员工列表' => U('index'), '员工修改' => U('edit', array('fieldset_id' => $fieldsetId)));
            $this->assign('breadCrumb', $breadCrumb);
            $this->assign('name', '修改');
            $departmentList = D('Human/Department') ->loadList();
            $this->assign('departmentList', $departmentList);
            $jobList = D('Human/Job') ->getInfo($info['job_id']);
            $this->assign('storeSearch',D('Functions/Store')->loadList());
            $this->assign('jobList', $jobList);   
            $this->assign('info', $info);
            $this->adminDisplay('info');
        }
        else
        {
            if ($model->saveData('edit'))
            {
                $this->success('员工列表修改成功！');
            }
            else
            {
                $msg = $model->getError();
                if (empty($msg))
                {
                    $this->error('员工列表修改失败');
                }
                else
                {
                    $this->error($msg);
                }
            }
        }
    }

    /**
     * 删除
     */
    public function del()
    {
        $employeeId = I('post.data');
        if (empty($employeeId))
        {
            $this->error('参数不能为空！');
        } 
        // 删除操作
        $model = D('employee');
        if ($model->delData($employeeId))
        {
            $this->success('员工删除成功！', U('index'));
        }
        else
        {
            $msg = $model->getError();
            if (empty($msg))
            {
                $this->error('员工删除失败！');
            }
            else
            {
                $this->error($msg);
            }
        }
    }

    /**
     *动态获取职位
     *
     */
    public function getAjaxJob(){

        $departmentId = intval($_POST['department_id']);
        $data=D('Job')->getJobByDepartmentId($departmentId);
        $this->ajaxReturn($data);
    }

    /**
     * 状态更改
     */
    public function state(){
        $state = I('post.data');
        $employeeId = I('get.employee_id');
        if (empty($employeeId) && empty($state))
        {
            $this->error('参数不能为空！');
        } 
        $data['employee_status'] = $state;
        $where['employee_id'] = $employeeId;
        //更新状态
        $model = D('employee');
        if($model->updateData($data,$where)){
            $this->success('员工状态更新成功！');
        }else{
            $this->error('员工状态更新失败！');
        }
    }
}

